<?php
Yii::app()->clientScript->registerCssFile(Yii::app()->request->baseUrl.'/css/log.css');
Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/scripts/diff_match_patch_uncompressed.js');
?>

<?php
$this->breadcrumbs=array(
	'Item'=>$this->createUrl('Item/view',array('id'=>$item->id)),
	$item->id
);
?>

<script language="JavaScript">
<!--
ValuesFactory = {
	oldValue : <?php echo $preModel->value; ?>,
	newValue : <?php echo $afterModel->value; ?>,

	getOldValue: function(key) {
		if (ValuesFactory.oldValue[key]) return ValuesFactory.oldValue[key].toString();
		return '';
	},
	getNewValue: function(key) {
		if (ValuesFactory.newValue[key]) return ValuesFactory.newValue[key].toString();
		return '';
	},
}
//-->
</script>

	<div class="span12">
		<h1>Difference of item "<b><?php echo $item->name ?></b>" between revision <?php echo $preModel->id ?> and  <?php echo $afterModel->id ?></h1>
	</div>

<div class="span2 well key-array" id="fields">
	<h3><b>Key array</b></h3>
	<?php $this->renderPartial('_keys', array(
			'keys'=>$keys,
			'new'=>$onlyAfter,
			'deleted'=>$onlyBefore,
			'remain'=>$both,
		));
	?>
</div>

<div>
	<h3><b><span style="margin-left: 20px;">Show Diff</span></b></h3>
	<div class="span10 show-diff" id="diff-wrp">
		<div class="span3 oldVersion" id="oldVersion"></div>
		<div class="span3 newVersion" id="newVersion"></div>
		<div class="span4 " id="bothVersion"></div>
	</div>

	<div class="span10">
		<table cellspacing="0" cellpadding="0">
			<tbody id="table">
			</tbody>
		</table>
	</div>
</div>

<script language="JavaScript">
<!--
function explode (delimiter, string, limit) {
	// Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.
	//
	// version: 1109.2015
	// discuss at: http://phpjs.org/functions/explode
	// +	 original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +	 improved by: kenneth
	// +	 improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +	 improved by: d3x
	// +	 bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// *	 example 1: explode(' ', 'Kevin van Zonneveld');
	// *	 returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
	// *	 example 2: explode('=', 'a=bc=d', 2);
	// *	 returns 2: ['a', 'bc=d']
	var emptyArray = {
		0: ''
	};

	// third argument is not required
	if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') {
		return null;
	}

	if (delimiter === '' || delimiter === false || delimiter === null) {
		return false;
	}

	if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') {
		return emptyArray;
	}

	if (delimiter === true) {
		delimiter = '1';
	}

	if (!limit) {
		return string.toString().split(delimiter.toString());
	}
	// support for limit argument
	var splitted = string.toString().split(delimiter.toString());
	var partA = splitted.splice(0, limit - 1);
	var partB = splitted.join(delimiter.toString());
	partA.push(partB);
	return partA;
}

jQuery(function($) {
	$('ul.fields li').click(function(e) {
		var a = $(this).children('a');
		var key = a.attr('rel');

		var newValue = ValuesFactory.getNewValue(key);
		var oldValue = ValuesFactory.getOldValue(key);

		var dmp = new diff_match_patch();
		var diff = dmp.diff_main(oldValue, newValue);
		console.log(diff);

		var html = dmp.diff_prettyHtml(diff);
		$("#diff-wrp").html(html);


		html=explode('&para;<br>',html,false);

		for (var x = 0; x < html.length; x++) {
			console.log(html[x]);
		  	html[x]=html[x]+'&para';
			$("<tr><td  class='span5 old'>"+html[x]+"  &nbsp </td><td class='span5 new'>"+html[x]+"  &nbsp </td></tr>").appendTo("#table");
		  }
		  $("#oldVersion .element").each(function(){
			  var text=$(this).text();
			  if(text=='')
			  	$('xxx').appendTo($(this));
		  })

		return;

//		$('#diff-wrp').html(html);
		$('#oldVersion').html(html);
		$('#newVersion').html(html);
		$('#bothVersion').html(html);
	});
});
//-->
</script>